package com.sc;

import org.junit.Test;

import java.util.Arrays;

/**
 * Created by Shen Chao on 2018-06-02-002.
 */
public class ThreePaiXu {
    private int[] a = {2, 3, 4, 1, 3, 4, 6, 7, 8, 9};

    //选择排序
    @Test
    public void choiceSort() {
        for (int i = 0; i < a.length; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[min]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }
        }
        System.out.println(Arrays.toString(a));
    }

    //冒泡排序
    @Test
    public void bubbleSort() {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[i] < a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }

    //快速排序
    private void quickSort(int[] a, int l, int r) {
        int left = l;
        int right = r;
        int temp;
        if (left <= right) {
            temp = a[left];
            while (left != right) {
                while (left < right && a[right] >= temp) {
                    right--;
                    a[left] = a[right];
                }
                while (left < right && a[left] <= temp) {
                    left++;
                    a[right] = a[left];
                }
            }
            a[left] = temp;
            quickSort(a, l, left - 1);
            quickSort(a, right + 1, r);
        }
    }

    //测试快速排序
    @Test
    public void testQuickSort() {
        quickSort(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a));
    }
}